from sqlalchemy import create_engine, Column, Integer, String, DateTime, Boolean
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy.orm import sessionmaker, Session
from sqlalchemy import func
from sqllite_pre import Base,get_db
from typing import List, Optional, Union
from pydantic import BaseModel, EmailStr

# 文件上传表
class File(Base):
    __tablename__ = "file"

    id = Column(Integer, primary_key=True, autoincrement=True)
    user_id = Column(Integer, nullable=False)
    username = Column(String(100), nullable=False)
    filename = Column(String(255), nullable=False)

def add_file_record(
    db: Session,
    user_id: int,
    username: str,
    filename: str
) -> None:
    """添加搜索记录"""
    new_record = File(
        user_id=user_id,
        username=username,
        filename=filename
    )
    db.add(new_record)
    db.commit()

def delete_file_record(db: Session, filename: str):
    #删除指定文件名的记录
    record = db.query(File).filter(File.filename == filename).first()
    # 执行删除
    db.delete(record)
    db.commit()

def get_user_file_records(db: Session, user_id: int) -> List[File]:
    #返回用户上传的文件列表
    records = db.query(File).filter(File.user_id == user_id).all()
    return records